package
{
	import com.flesler.componentas.components.events.EventsComponent;
	import com.flesler.componentas.components.position.PositionComponent;
	import com.flesler.componentas.core.Entity;
	import com.flesler.componentas.test.components.*;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class Main extends Sprite
	{
		private var entity:Entity;
		
		public function Main()
		{
			init();
		}
		
		private function init():void
		{
			initializeEntity();
			testPosition();
			testEvents();
		}
		
		private function initializeEntity():void
		{
			entity = new Entity();
			entity.register(new EventsComponent());
			entity.register(new PositionComponent());
			entity.register(new PositionDisplayComponent());
			entity.initialize();
		}
		
		private function testPosition():void
		{
			entity.tracePosition();
			
			entity.x = 10;
			entity.y = -10;
			
			entity.tracePosition();
			
			try
			{
				var ret:Object = entity.nonExistentAttribute;
				// Never reached
				trace('nonExistentAttribute is', ret);
			} 
			catch(e:Error)
			{
				trace('ERROR:', e.message);
			}
		}
		
		private function testEvents():void
		{
			entity.addEventListener('foo', traceEvent);
			entity.dispatchEvent(new Event('foo'));
			trace('foo dispatch is expected by now');
		}
		
		private function traceEvent(e:Event):void
		{
			trace('dispatched foo event');
		}
	}
}